package cn.edu.neu.lab603.cloudeval

import cn.edu.neu.lab603.cloudeval.entity.{CloudTask, DataCenter, PhysicalMachine, User}
import cn.edu.neu.lab603.cloudeval.strategy.{AllocationStrategy, ReallocationStrategy}
import cn.edu.neu.lab603.des.{SimEvent, Simulation}

import scala.collection.mutable.ListBuffer

/** 云计算相关的函数。
  *
  * Created by yfwz100 on 16/9/19.
  */
class CloudEval extends Simulation[CloudEval] {

  val dataCenters = new ListBuffer[DataCenter]
  val users = new ListBuffer[User]

  override def run(clockAtMost: Int): Unit = {
    // 添加初始化事件到队列中
    dataCenters.foreach(_.initDataCenterLayout())
    users.foreach(_.submitCloudlets())

    // 调用 DES 引擎的仿真环境
    super.run(clockAtMost)
  }

  def createUser[E <: CloudTask](cloudlets: Iterable[E]): User = {
    val user = new User(this, cloudlets.map(_.asInstanceOf[CloudTask]).to[ListBuffer])
    users += user
    user
  }

  def createDataCenter(allocationStrategy: AllocationStrategy,
                       reallocationStrategy: ReallocationStrategy,
                       pmList: Iterable[PhysicalMachine] = List.empty): DataCenter = {
    val dc = new DataCenter(this, allocationStrategy, reallocationStrategy)
    dc.addPhysicalMachines(pmList)
    dataCenters += dc
    dc
  }

  override def shutdownEvent: SimEvent[CloudEval] = new DataCenter.ShutdownEvent
}
